<?php

namespace app\api\controller;

use think\Response;
use think\facade\Db;
use think\Request;

class Door extends Wap
{
    /**
     * 开门
     * @param Request $request
     * @throws DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(Request $request)
    {
        ini_set('max_execution_time', 2);
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();

            if (!$post['door_id']) {
                $arr['code'] = 101;
                $arr['message'] = '抱歉，当前服务未配置门禁';
                Response::create($arr, 'json')->send();
                exit();
            }
            $lat = 0;
            if (isset($post['lat'])) {
                $lat = $post['lat'];    //商户id
                $lng = $post['lng'];    //商户id
            }
            $order = Db::name('customer_order')->where('order_on', $post['order_on'])->find();
            $appkey = Db::name('customer_set_cabinet')->where('store_id', $order['store_id'])->find();
            //营业时间
            if (!$appkey) {
                $arr['code'] = 101;
                $arr['message'] = '配置信息错误';
                Response::create($arr, 'json')->send();
                exit();
            }
            $room = Db::name('mini_room_list')->where('id', $order['store_id'])->find();
//            if ($appkey['door_distance'] > 0) {
//                // 计算 距离
//                if ($lat > 0) {
//                    $distance = Wap::distance($lat, $lng, $room['latitude'], $room['longitude']);
//                    if ($distance > $appkey['door_distance'] * 1000) {
//                        $arr['code'] = 101;
//                        $arr['message'] = '不在开门范围内';
//                        Response::create($arr, 'json')->send();
//                        exit();
//                    }
//                }
//            }
            $hours_time = $this->time();
            $hours_add_time = $hours_time['hours_add_time'];
            $hours_end_time = $hours_time['hours_end_time'];
            $time_add = $hours_time['time_add'];
            $add_time = strtotime(date('Y-m-d ', time()) . ' ' . $hours_add_time[$room['hours_add_time'] + 1] . ':' . $time_add[$room['hours_add_time_fen']]);
            $end_time = strtotime(date('Y-m-d', time()) . ' ' . $hours_end_time[$room['hours_end_time'] + 1] . ':' . $time_add[$room['hours_end_time_fen']]);
            $door_list_data = $appkey;
            $door_list_data['equipment_id'] = $post['door_id'];
            $lamp_add = time() + $appkey['time'] * 60;  //可提前开门时间
            if ($lamp_add < $order['add_time']) {
                $arr['code'] = 101;
                $arr['message'] = '大于可提前开门时间'.$appkey['time'];
                Response::create($arr, 'json')->send();
                exit();
            }
            if ($add_time > $end_time) {
                $end_time = $end_time + 86400;
            }
            if ($lamp_add < $add_time) {
                $arr['code'] = 101;
                $arr['message'] = '抱歉不再营业时间' . $add_time;
                Response::create($arr, 'json')->send();
                exit();
            }
            if (time() > $end_time) {
                $arr['code'] = 101;
                $arr['message'] = '抱歉不再营业时间内';
                Response::create($arr, 'json')->send();
                exit();
            }

            $door = Db::name('customer_door_list')->where('door_id', $post['door_id'])->find();

            if($order['status']==1){
                if ($door['pid'] > 0) {
                    $cleaninglist = Db::name('customer_order_cleaning')->where('mark', 1)->where('status', '<', 4)->where('add_time', '<', time())->where('seat_id', '=', $order['seat_id'])->select();
                    if (count($cleaninglist) > 0) {
                        foreach ($cleaninglist as $i => $v) {

                            $order = Db::name('customer_order')->where('order_on', $v['order_on'])->where('mark', 1)->find();
                            if ($order) {
                                $arr['code'] = 101;
                                $arr['message'] = '抱歉,当前房间未打扫。不可开门';
                                Response::create($arr, 'json')->send();
                                exit();
                            }
                        }
                    }
                } else {
                    $doordata = Db::name('customer_door_list')->where('pid', $door['id'])->find();

                    if ($doordata) {
                        $cleaninglist = Db::name('customer_order_cleaning')->where('add_time', '<', time())->where('mark', 1)->where('status', '<', 4)->where('seat_id', '=', $order['seat_id'])->select();
                        if (count($cleaninglist) > 0) {
                            $arr['code'] = 101;
                            $arr['message'] = '抱歉,当前房间未打扫。不可开门';
                            Response::create($arr, 'json')->send();
                            exit();
                        }
                    }
                }

            }
            $res=    self::newopen($door['door_id'], 0);
            if($res['code']=='200'){
                $arr['message'] = '开锁成功';
                $arr['code'] = 200;
                Response::create($arr, 'json')->send();
                exit();

            }else{
                $arr['message'] = '开锁失败,'.$res['msg'];
                $arr['code'] = 101;
                Response::create($arr, 'json')->send();
                exit();

            }




        }
    }


    /**
     * 开门
     * @param Request $request
     * @throws DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */

    public
    function seat(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();

            if (!$post['door_id']) {
                $arr['code'] = 101;
                $arr['message'] = '抱歉，当前服务未配置门禁';
                Response::create($arr, 'json')->send();
                exit();
            }
            $lat = 0;
            if (isset($post['lat'])) {
                $lat = $post['lat'];    //商户id
                $lng = $post['lng'];    //商户id
            }

            $order = Db::name('customer_seat_order')->where('order_on', $post['order_on'])->find();

            $appkey = Db::name('customer_set_cabinet')->where('store_id', $order['store_id'])->find();
            //营业时间
            if (!$appkey) {
                $arr['code'] = 101;
                $arr['message'] = '配置信息错误';
                Response::create($arr, 'json')->send();
                exit();
            }
            $room = Db::name('mini_room_list')->where('id', $order['store_id'])->find();
            if ($appkey['door_distance'] > 0) {
                // 计算 距离
                if ($lat > 0) {
                    $distance = Wap::distance($lat, $lng, $room['latitude'], $room['longitude']);
                    if ($distance > $appkey['door_distance'] * 1000) {
                        $arr['code'] = 101;
                        $arr['message'] = '不在开门范围内';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                }
            }
            $hours_time = $this->time();
            $hours_add_time = $hours_time['hours_add_time'];
            $hours_end_time = $hours_time['hours_end_time'];
            $time_add = $hours_time['time_add'];
            $add_time = strtotime(date('Y-m-d ', time()) . ' ' . $hours_add_time[$room['hours_add_time'] + 1] . ':' . $time_add[$room['hours_add_time_fen']]);
            $end_time = strtotime(date('Y-m-d', time()) . ' ' . $hours_end_time[$room['hours_end_time'] + 1] . ':' . $time_add[$room['hours_end_time_fen']]);
            $door_list_data = $appkey;
            $door_list_data['equipment_id'] = $post['door_id'];

            $lamp_add = time() + $appkey['door_time'] * 60;
            if ($lamp_add < $order['add_time']) {
                $arr['code'] = 101;
                $arr['message'] = '抱歉不再预约时间内';
                Response::create($arr, 'json')->send();
                exit();
            }
            if ($lamp_add < $order['add_time']) {
                $arr['code'] = 101;
                $arr['message'] = '抱歉不再预约时间内2';
                Response::create($arr, 'json')->send();
                exit();
            }
            if ($add_time > $end_time) {
                $end_time = $end_time + 86400;
            }
            if ($lamp_add < $add_time) {
                $arr['code'] = 101;
                $arr['message'] = '抱歉不再预约时间内3';
                Response::create($arr, 'json')->send();
                exit();
            }
            if (time() > $end_time) {
                $arr['code'] = 101;
                $arr['message'] = '抱歉不再预约时间内4';
                Response::create($arr, 'json')->send();
                exit();
            }
            $door = Db::name('customer_door_list')->where('id', $post['door_id'])->find();
            if (self::open($door_list_data, $order['store_id'], $user['id'])) {

                if ($door['pid'] > 0) {
                    if ($order['lamp_id'] != 1) {
                        if (true == $this->opens($door_list_data, 03)) {
                            // 记录信息
                            $doorList = array(
                                'admin_id' => $order['store_id'],
                                'create_time' => time(),
                                'user_id' => $user['id'],
                                'status' => 1,
                                'order_on' => $post['order_on'],
                                'door_id' => $post['door_id'],
                            );
                            $lamp['lamp_id'] = 1;
                            Db::name('customer_order')->where('order_on', $post['order_on'])->update($lamp);
                            
                            Db::name('mini_admin_lamp_list')->insert($doorList);
                            $arr['message'] = '开锁成功';
                            $arr['code'] = 200;
                            Response::create($arr, 'json')->send();
                            exit();
                        }
                    }
                }
                if ($door['pid'] == 0) {
                    $door = Db::name('customer_door_list')->where('pid', $door['id'])->find();
                    if (!$door) {
                        if ($order['lamp_id'] != 1) {
                            if (true == $this->opens($door_list_data, 03)) {
                                // 记录信息
                                $doorList = array(
                                    'admin_id' => $order['store_id'],
                                    'create_time' => time(),
                                    'user_id' => $user['id'],
                                    'status' => 1,
                                    'order_on' => $post['order_on'],
                                    'door_id' => $post['door_id'],
                                );
                                $lamp['lamp_id'] = 1;
                                Db::name('customer_order')->where('order_on', $post['order_on'])->update($lamp);
                                Db::name('mini_admin_lamp_list')->insert($doorList);
                                $arr['message'] = '开锁成功';
                                $arr['code'] = 200;
                                Response::create($arr, 'json')->send();
                                exit();
                            }
                        }

                    }

                }
            }
            $arr['message'] = '开锁失败,请联系客服';
            $arr['code'] = 101;
            Response::create($arr, 'json')->send();
            exit();
        }


    }
}